package de.tum.datatypes;

import java.util.List;

import de.tum.Misc.Exceptions;
import de.tum.Misc.Exceptions.numberOfMPChoiceBoxesNotMatchingException;
import de.tum.Misc.Exceptions.numberOfMPChoiceQuestionsNotMatchingException;

public class MultipleChoiceExerciseEvaluation {

	/**
	 * the index of the current
	 */
	private int index;
	private double pointsPerRightSelection;
	private int numberOfMultipleChoiceQuestions;
	private List<boolean[]> questionsEvaluation;
	private List<boolean[]> mpChoiceQuestions;
	
	
	public MultipleChoiceExerciseEvaluation(int index, double pointsPerRightSelection, 
			int numberOfMultipleChoiceQuestions, List<boolean[]> questionsEvaluation,
			List<boolean[]> mpChoiceQuestions){
		this.index = index;
		this.pointsPerRightSelection = pointsPerRightSelection;
		this.numberOfMultipleChoiceQuestions = numberOfMultipleChoiceQuestions;
		this.questionsEvaluation = questionsEvaluation;
		this.setMpChoiceQuestions(mpChoiceQuestions);
	}


	public double calculateArchievedPoints() throws numberOfMPChoiceQuestionsNotMatchingException, numberOfMPChoiceBoxesNotMatchingException {
		if(questionsEvaluation.size() != mpChoiceQuestions.size()){
			throw  new numberOfMPChoiceQuestionsNotMatchingException(
					"Size of the expected Multiple Choice Questions ("+mpChoiceQuestions.size()+") " +
							"does not match to the number of the recognized questions ("+questionsEvaluation.size()+")");
		}
		
		double archievedPoints = 0;
		
		for(int i=0; i<questionsEvaluation.size(); i++){
			
			int nbrOfMatchingAnswers = 0;
			int nbrOfWrongAnswers = 0;
			
			boolean[] correctAnswers = questionsEvaluation.get(i);
			boolean[] givenAnswers = mpChoiceQuestions.get(i);
			
			
			if(correctAnswers.length != givenAnswers.length){
				throw  new numberOfMPChoiceBoxesNotMatchingException(
						"Size of the expected multiple choice boxes ("+mpChoiceQuestions.size()+") " +
								"does not match to the number of the recognized boxes ("+questionsEvaluation.size()+")");
			}
			
			for(int j=0; j<correctAnswers.length; j++){
				if(correctAnswers[j] == givenAnswers[j]){
					nbrOfMatchingAnswers++;
				}else{
					nbrOfWrongAnswers++;
				}
			}
			
			int numberOfBoxes = givenAnswers.length;
			
			archievedPoints += (nbrOfMatchingAnswers * this.pointsPerRightSelection) - (nbrOfWrongAnswers*this.pointsPerRightSelection);
			
		}
		
		if(archievedPoints<0)
			return 0;
		
		return archievedPoints;
		
	}
	
	
	
	
	
	
	
	
	
	
	
	/*
	 * GETTERS & SETTERS
	 */
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}
	
	public double getPointsPerRightSelection() {
		return pointsPerRightSelection;
	}

	public void setPointsPerRightSelection(double pointsPerRightSelection) {
		this.pointsPerRightSelection = pointsPerRightSelection;
	}

	public List<boolean[]> getQuestions() {
		return questionsEvaluation;
	}

	public void setQuestions(List<boolean[]> questions) {
		this.questionsEvaluation = questions;
	}


	/**
	 * @return the numberOfMultipleChoiceQuestions
	 */
	public int getNumberOfMultipleChoiceQuestions() {
		return numberOfMultipleChoiceQuestions;
	}


	/**
	 * @param numberOfMultipleChoiceQuestions the numberOfMultipleChoiceQuestions to set
	 */
	public void setNumberOfMultipleChoiceQuestions(
			int numberOfMultipleChoiceQuestions) {
		this.numberOfMultipleChoiceQuestions = numberOfMultipleChoiceQuestions;
	}


	/**
	 * @return the mpChoiceQuestions
	 */
	public List<boolean[]> getMpChoiceQuestions() {
		return mpChoiceQuestions;
	}


	/**
	 * @param mpChoiceQuestions the mpChoiceQuestions to set
	 */
	public void setMpChoiceQuestions(List<boolean[]> mpChoiceQuestions) {
		this.mpChoiceQuestions = mpChoiceQuestions;
	}


	
}
